local skill = fk.CreateSkill {
  name = "pear_skill",
}

skill:addEffect("cardskill", {
  prompt = function(self, player, selected_cards)
    local card = Fk:cloneCard("pear")
    card:addSubcards(selected_cards)
    local max_num = self:getMaxTargetNum(player, card)
    if max_num > 1 then
      local num = #table.filter(Fk:currentRoom().alive_players, function (p)
        return p ~= player and not player:isProhibited(p, card)
      end)
      max_num = math.min(num, max_num)
    end
    return max_num > 1 and "#pear_skill_multi:::" .. max_num or "#pear_skill"
  end,
  target_num = 1,
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select ~= player and to_select:isWounded()
  end,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    if effect.to:isWounded() and not effect.to.dead then
      room:recover{
        who = effect.to,
        num = 1,
        card = effect.card,
        recoverBy = effect.from,
        skillName = skill.name,
      }
    end
  end,
})

skill:addEffect(fk.AfterCardsMove, {
  global = true,
  priority = 0.1,
  --mute = true,
  can_trigger = function(self, event, target, player, data)
    if player.phase == Player.Play or player.dead then return end
    for _, move in ipairs(data) do
      if move.from and move.from == player then
        local cards = {}
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if info.fromArea == Card.PlayerHand and Fk:getCardById(id).trueName == "pear" then
            table.insertIfNeed(cards, id)
          end
        end
        if #cards > 0 then
          data.extra_data = data.extra_data or {}
          data.extra_data.pear = #cards
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local pear_recover = data.extra_data.pear or {}
    player.room:recover({
      who = player,
      num = pear_recover,
      recoverBy = player,
      skillName = self.name
    })
  end,
})

return skill
